﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLVMBAYDatacess;
using QLVMBAYDomain;

namespace QLVMBAY.Pages
{
    public partial class pLichBay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var getSB = SanBayDA.Inst.GetAll(false);
                LoadDDl(ddlSanBayDen, getSB);
                LoadDDl(ddlSanBayDi, getSB);
                ClientComm.Ints.LoadNgay(cboxNgay, true);
                ClientComm.Ints.LoadThang(cboxThang, true);
            }

        }
        void LoadDDl(DropDownList ddl,List<SanBay>Lst)
        {
            ddl.DataSource = Lst;
            ddl.DataTextField = "TenSanBay";
            ddl.DataValueField = "MaSanBay";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("Chọn", "0"));
 
        }

        protected void btnTim_Click(object sender, EventArgs e)
        {
            if (ddlSanBayDen.SelectedValue != "0" || ddlSanBayDi.SelectedValue != "0" || (cboxNgay.SelectedValue != "0" && cboxThang.SelectedValue != "0"))
            {
                var lst = new List<ChuyenBay>();
                bool DKKep = false;
                if (ddlSanBayDen.SelectedValue != "0")
                {
                    DKKep = true;
                    lst = ChuyenBayDA.Inst.GetListCBayByMaTenSB(ddlSanBayDen.SelectedValue, false, true); 
                }
                else if (ddlSanBayDi.SelectedValue != "0")
                {
                    if (DKKep)
                    {
                        lst = lst.Where(r => r.ItemTuyenBay.ItemSanBayDi.MaSanBay == ddlSanBayDi.SelectedValue).ToList();
                    }
                    else
                    {
                        lst = ChuyenBayDA.Inst.GetListCBayByMaTenSB(ddlSanBayDi.SelectedValue, true, true);
                        DKKep = true;
                    }
 
                }
                else if (cboxNgay.SelectedValue != "0" && cboxThang.SelectedValue != "0")
                {
                    var dTime = ClientComm.Ints.ToDate(cboxNgay.SelectedValue + "/" + cboxThang.SelectedValue);
                    if (DKKep)
                    {
                        
                        lst = lst.Where(r => r.NgayBay == dTime).ToList();
                    }
                    else
                    {
                        lst = ChuyenBayDA.Inst.GetListCBayByDK(DIEU_KIEN_TIM.GIA_LON_HON_BANG, 0, "", dTime, true);
                    }
                }
                grdChuyenBayDi.DataSource = lst;
                grdChuyenBayDi.DataBind();

            }
            else
            {
                Page.RegisterStartupScript("Err",
                                                      "<script language='javascript'>alert('Vui lòng chọn điều kiện xem lịch bay.');</script>");
            }

        }

        protected void grdChuyenBayDi_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItemIndex != -1)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    var ChBay = (ChuyenBay)e.Row.DataItem;
                    var lbl0 = e.Row.FindControl("lblSanBayDi") as Label;
                    lbl0.Text = ChBay.ItemTuyenBay.ItemSanBayDi.TenSanBay;
                    var lbl = e.Row.FindControl("lblSanBayDen") as Label;
                    lbl.Text = ChBay.ItemTuyenBay.ItemSanBayDen.TenSanBay;

                    var lbl1 = e.Row.FindControl("lblTenHang") as Label;
                    lbl1.Text = ChBay.ItemHangHangKhong.TenHang;

                    var lbl11 = e.Row.FindControl("lblNgayBay") as Label;
                    lbl11.Text = ChBay.NgayBay.ToString("dd/MM/yyyy");

                    var lbl2 = e.Row.FindControl("lblTgKhoiHanh") as Label;
                    lbl2.Text = ChBay.TgKhoiHanh.ToString("hh':'mm");

                    var lbl3 = e.Row.FindControl("lblThoiGianBay") as Label;
                    if (ChBay.ThoiGianBay.Days > 0)
                    {
                        lbl3.Text = ChBay.ThoiGianBay.ToString("d'd hh'h'mm'p'");
                    }
                    else if (ChBay.ThoiGianBay.Hours > 0)
                    {
                        lbl3.Text = ChBay.ThoiGianBay.ToString("hh'h'mm'p'");
                    }
                    else
                    {
                        lbl3.Text = ChBay.ThoiGianBay.ToString("mm'p'");
                    }
                }
            }
        }
    }
}